<?
include("sqlconnect.php");


session_start();
$user_id = $_SESSION['usrid'];


if(isset($_POST['ld']))
$parameter = $_POST['ld'];
else echo 'Ajax Query Error';

if($parameter == 'creategroup')
{

$grpname= $_POST['newgroupname'];
$grpproducts = $_POST['groupproducts'];
$grpdesc = $_POST['newgroupdesc'];
$query = "INSERT INTO  user_groups(
group_id,
group_name,
group_creator,
group_desc,
group_products
)
VALUES (
NULL ,  '$grpname',  '$user_id',  '$grpdesc',  '$grpproducts')";

$result= mysql_query($query) or die(mysql_error());

echo('<div id="grouppost">
	          <img src="Images/group.png"/>
  			   <h1><div id="grpname" class="postbox">Group Name:'.$grpname.'</div><br><br></h1>    
            <div id="grpmembers" class="stdlink">Group Members</div><br>    
   			<div id="grppostlist" class="postbox">
            </div>
   			<div id="nwgrppost">         
      		<textarea name="newpostgrp" cols="40" rows="5" id="newpostgrp"></textarea><br>
      		<input name="postgrp" type="button" id="postgrp" value="post it" onclick="newgrouppost()"/>
   			</div>   
			   </div>

	');


$query= "SELECT * FROM user_groups where group_name = '".$grpname."'";
$result= mysql_query($query) or die(mysql_error());
$row = mysql_fetch_object($result);
$_SESSION['currgrpid'] = $row->group_id;

echo'<h1>aksjdnaaslkdal</h1>';
   

}

if($parameter == 'addthisgrpfriend')
{
   
   
   $fid = $_POST['nwfid'];
   $currgrpid = $_SESSION['currgrpid'];   

   
   $query = "INSERT INTO group_data(
   user_id,group_id) 
   VALUES ('$fid','$currgrpid')";

   $result= mysql_query($query) or die(mysql_error());

}


if($parameter == 'nwgrppost')
{
   
   $currgrpid = $_SESSION['currgrpid'];   
   $newpost = $_POST['nwp'];
  
   $f_name = $_SESSION['fname'];
   $query = "INSERT INTO group_content(
   group_id,post,poster) 
   VALUES ('$currgrpid','$newpost','$user_id')";
   $result= mysql_query($query) or die(mysql_error());

   echo '<div id="oldpost" class="postbox">         

   <div id="u'.$user_id.'" class="namedisplay">'.$f_name.'</div><br><br>

   <div id="post">'.$newpost.'</div>
   </div><br><br>';


}


if($parameter == 'listallhisgroups')
{
   

$query= "SELECT * FROM user_groups where group_creator = '".$user_id."'";
$result= mysql_query($query) or die(mysql_error());
$i=0;
           while($row = mysql_fetch_object($result))
            {
               $group_list[$i]= $row->group_name;
               $group_idlist[$i]= $row->group_id;
               echo '<li class="smenu"> <span id="grp'.$group_idlist[$i].'" onclick="viewgroup(this.id)" > '.$group_list[$i].' </span></li>';  
            $i++;   
            }

$query= "SELECT * FROM group_data where user_id = '".$user_id."'";
$result= mysql_query($query) or die(mysql_error());
           
           while($row = mysql_fetch_object($result))
            {
               $temp = $row->group_id;
               $group_idlist[$i]= $temp;
               $query1 = "SELECT * FROM user_groups where group_id = '".$temp."'";
               $result1= mysql_query($query1) or die(mysql_error());
               
               if(mysql_num_rows($result1) > 0)
               {
                  $row1 = mysql_fetch_object($result1);
                  $group_list[$i]= $row1->group_name;
                  echo '<li class="smenu"> <span id="grp'.$group_idlist[$i].'" onclick="viewgroup(this.id)" > '.$group_list[$i].' </span></li>';  
                  $i++;   
               }else break;
            }

$i=0;

/*foreach($group_list as $i => $grp)            
{
   
  echo '<li class="smenu"> <span id="grp'.$group_idlist[$i].'" onclick="viewgroup(this.id)" > '.$grp.' </span></li>';       

}*/


}



if($parameter == 'viewagroup')
{
   
   $grpid = $_POST['grpid'];
   $_SESSION['currgrpid'] = $grpid;

   $query= "SELECT * FROM user_groups where group_id = '".$grpid."'";
   $result= mysql_query($query) or die(mysql_error());
   $row = mysql_fetch_object($result);
   $grpname = $row->group_name;

   $query1= "SELECT * FROM group_content where group_id = '".$grpid."'";
   $result1= mysql_query($query1) or die(mysql_error());
   $grp_post = array();
   $grp_poster = array();
   $i=0;
      while($row1 = mysql_fetch_object($result1))
      {
          $grp_post[$i]= $row1->post;
          $grp_poster[$i] = $row1->poster;
          $i++;
      }


  echo '<div id="grouppost" class="postbox">
          <img src="Images/group.png"/>     
         <h1><div id="grpname" class="postbox">Group Name:'.$grpname.'</div></h1><br>
         <div id="grpmembers" class="stdlink">Group Members</div><br>
         <div id="grppostlist" class="postbox">';

   $i=0;
   foreach($grp_post as $i => $grppost)      
   {
      
      $query2= "SELECT * FROM users where user_id = '".$grp_poster[$i]."'";
      $result2= mysql_query($query2) or die(mysql_error());
      $row2 = mysql_fetch_object($result2);
      $f_name = $row2->user_fname;


      echo' <div id="oldpost'.$i.'" class="subpostbox">         
            <div id="u'.$grp_poster[$i].'" class="namedisplay">'.$f_name.'</div><br><br>
            <div id="post">'.$grppost.'</div>
            </div><br><br>';

   }

   echo ' </div>';

   echo '<div id="nwgrppost" class = "subpostbox">         
            <textarea name="newpostgrp" cols="40" rows="5" id="newpostgrp"></textarea><br>
            <input name="postgrp" type="button" id="postgrp" value="post it" onclick="newgrouppost()"/>
            </div>   
            </div>';

}


if($parameter == 'editgrpmemebers')
{
   //$grpid = $_POST['grpid'];
   $grpid = $_SESSION['currgrpid'];

   $query= "SELECT * FROM group_data where group_id = '".$grpid."'";
   $result= mysql_query($query) or die(mysql_error());

   $i=0;

   echo'<div id="grpmemberslist" class="box">         
      <h1>Current Group Memebers</h1><br>
      <form action="" method="post" id="grpmemberslistform" >';
   while($row = mysql_fetch_object($result))
   {
   
      $grpmem[$i] = $row->user_id;

      $query2= "SELECT * FROM users where user_id = '".$grpmem[$i]."'";
      $result2= mysql_query($query2) or die(mysql_error());
      $row2 = mysql_fetch_object($result2);
      $f_name[$i] = $row2->user_fname;

   
    echo'<label class ="rowlabel" id="l'.$grpmem[$i].'"> 
   <span class="rowleft"><input type="checkbox" id="wp'.$grpmem[$i].'" value="'.$f_name[$i].'" checked onchange="editgrpmemebers(this.id)"/> </span>
      <input id="p'.$grpmem[$i].'" type="text" class="input_text" readonly="true" size="70"   value="'.$f_name[$i].'" /><br/> 
      </label>';

   $i++;

   }

    echo'</form>
      </div>';  
}


if($parameter =='removefromgroup')
{
   $gpid = $_SESSION['currgrpid'];
   $fd = $_POST['fd'];
   
   $query = "DELETE * FROM group_data WHERE user_id ='$fd' AND group_id='$gpid'";
   mysql_query($query2) or die(mysql_error());

}


?>